package com.huan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huan.VO.AddArticleDto;
import com.huan.VO.CommentVo;
import com.huan.VO.PageVo;
import com.huan.constant.SystemConstants;
import com.huan.domain.ResponseResult;
import com.huan.domain.entity.Article;
import com.huan.domain.entity.ArticleTag;
import com.huan.domain.entity.Comment;
import com.huan.mapper.CommentMapper;
import com.huan.service.CommentService;
import com.huan.service.UserService;
import com.huan.utils.BeanCopyUtil;
import com.huan.utils.SecurityUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-06-07 20:41:58
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private UserService userService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
//        我们需要返回username，以及toCommentUserName
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(commentType.equals("0"),Comment::getArticleId,articleId);
        wrapper.eq(Comment::getType,commentType);
//        wrapper.orderByDesc(Comment::getCreateTime);
        Page<Comment> commentPage = new Page<>(pageNum,pageSize);
        Page<Comment> page = page(commentPage, wrapper);
        List<Comment> list = page.getRecords();
        List<CommentVo> voList = BeanCopyUtil.resultAllVo(list, CommentVo.class);
        List<CommentVo> collect = voList.stream()
                .filter(commentVo -> {
                    if (commentVo.getRootId()!=-1){
                        return false;
                    }else {return true;}
                })
                .map(commentVo -> {
                    commentVo.setUsername(userService.getById(commentVo.getCreateBy()).getNickName());
                    return commentVo;
                }).collect(Collectors.toList());
        List<Object> collect1 = collect.stream()
                .map((Function<CommentVo, Object>) commentVo -> {
                    LambdaQueryWrapper<Comment> wrapper1 = new LambdaQueryWrapper<>();
                    wrapper1.eq(Comment::getRootId, commentVo.getId());
//                    wrapper1.orderByDesc(Comment::getCreateTime);
                    List<Comment> list1 = list(wrapper1);
                    List<CommentVo> commentVos = BeanCopyUtil.resultAllVo(list1, CommentVo.class);
                    commentVos.stream().map(commentVo1 -> {
                        commentVo1.setToCommentUserName(userService.getById(commentVo1.getToCommentUserId()).getNickName());
                        commentVo1.setUsername(userService.getById(commentVo1.getCreateBy()).getNickName());
                        return commentVo1;
                    }).collect(Collectors.toList());
                    commentVo.setChildren(commentVos);
                    return commentVo;
                }).collect(Collectors.toList());
        PageVo pageVo = new PageVo(collect1,page.getTotal());


        return ResponseResult.okResult(pageVo);
    }

        @Override
        public ResponseResult addComment(Comment comment) {
            addCA(comment);
//            save(comment);
            return ResponseResult.okResult();
        }
        public void addCA(Comment comment){
            rabbitTemplate.convertAndSend("commentExchange", "#", comment);
        }
    @RabbitListener(queues = "commentExchange")
    public ResponseResult receive(Comment comment) {
        save(comment);
        return ResponseResult.okResult();
    }
}
























